/**
 * 纹理类 实现纹理的加载 绘制等
 * @date   2011-3-21
 * @author tianye
 */
package cn.edu.dlut.WingGame.Graphic;

import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import cn.edu.dlut.WingGame.BaseType.WingColor;

public class GLTexture {
	public GLTexData mData = new GLTexData();
	private float maxU = 1.0f;
	private float maxV = 1.0f;

	public void creatTexture(int ResourceID) {
		mData.isPath = false;
		mData.isAlpha = false;
		mData.mResourceID = ResourceID;

	}

	public void creatTexture(String Path) {
		mData.isPath = true;
		mData.isKeyColor = false;
		mData.mPath = Path;
	}

	public void creatTexture(int ResourceID, WingColor keycolor) {
		mData.isPath = false;
		mData.isKeyColor = true;
		mData.keyColor = keycolor;
		mData.mResourceID = ResourceID;

	}

	public void creatTexture(String Path, WingColor keycolor) {
		mData.isPath = true;
		mData.isKeyColor = true;
		mData.keyColor = keycolor;
		mData.mPath = Path;
	}

	public void checkBind()// 检查是否生成纹理并第一次绑定
	{
		try {
			if (mData.isPath) {
				String tPath = mData.mPath;
				GLTexContainer.creatTexture(tPath, this);
			} else {
				int tResourceID = mData.mResourceID;
				GLTexContainer.creatTexture(tResourceID, this);
			}
			maxU = mData.mWidth / (float) mData.mPow2Width;
			maxV = mData.mHeight / (float) mData.mPow2Height;
			mData.isBind = true;
		} catch (Exception e) {
			mData.isBind = false;
		}

	}

	// 绘制到屏幕上
	public void draw(float x, float y) {
		draw(x, y, mData.mWidth, mData.mHeight);
	}

	public void draw(float x, float y, float width, float height) {
		if (!mData.isBind) {
			checkBind();
			return;
		}
		if (mData.isAlpha)
			GLDevice.enableBlend();
		GLDevice.viewTranslate(0, 0);
		GLDevice.getGL().glColor4f(1.0f, 1.0f, 1.0f, GLDevice.getAlpha());
		GLDevice.getGL().glEnable(GL10.GL_TEXTURE_2D);
		GLDevice.getGL().glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		GLDevice.getGL().glEnableClientState(GL10.GL_VERTEX_ARRAY);

		// 绑定
		GLDevice.getGL().glBindTexture(GL10.GL_TEXTURE_2D, mData.mTextureID);

		// 映射
		FloatBuffer verticleBuffer = FloatBuffer.wrap(new float[] { x, y,
				x + width, y, x, y + height, x + width, y + height, });
		FloatBuffer coordBuffer = FloatBuffer.wrap(new float[] { 0, 0, maxU, 0,
				0, maxV, maxU, maxV, });

		GLDevice.getGL().glTexCoordPointer(2, GL10.GL_FLOAT, 0, coordBuffer);
		GLDevice.getGL().glVertexPointer(2, GL10.GL_FLOAT, 0, verticleBuffer);
		GLDevice.getGL().glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

		GLDevice.getGL().glDisableClientState(GL10.GL_VERTEX_ARRAY);
		GLDevice.getGL().glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		GLDevice.getGL().glDisable(GL10.GL_TEXTURE_2D);
		GLDevice.getGL().glLoadIdentity();

		if (mData.isAlpha)
			GLDevice.disableBlend();
	}

	public void drawEx(float x, float y, float rotation, float alpha) {
		drawEx(x, y, mData.mWidth, mData.mHeight, 0, 0, mData.mWidth,
				mData.mHeight, rotation, alpha);
	}

	public void drawEx(float x, float y, float width, float height,
			float rotation, float alpha) {
		drawEx(x, y, width, height, 0f, 0f, mData.mWidth, mData.mHeight,
				rotation, alpha);
	}

	public void drawEx(float x, float y, float width, float height,
			float src_x, float src_y, float src_width, float src_height,
			float rotation, float alpha) {
		if (!mData.isBind) {
			checkBind();
			return;
		}
		if (mData.isAlpha || alpha != 1)
			GLDevice.enableBlend();

		GLDevice.getGL().glEnable(GL10.GL_TEXTURE_2D);
		GLDevice.getGL().glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		GLDevice.getGL().glEnableClientState(GL10.GL_VERTEX_ARRAY);

		GLDevice.viewTranslate(x, y);
		GLDevice.getGL().glRotatef(-rotation, 0, 0, 1);
		// 设置透明度
		GLDevice.getGL().glColor4f(1.0f, 1.0f, 1.0f,
				alpha * GLDevice.getAlpha());
		// 绑定
		GLDevice.getGL().glBindTexture(GL10.GL_TEXTURE_2D, mData.mTextureID);
		// 映射
		FloatBuffer verticleBuffer = FloatBuffer.wrap(new float[] { 0, 0,
				width, 0, 0, height, width, height, });
		float tminU = src_x / mData.mPow2Width;
		float tminV = src_y / mData.mPow2Height;
		float tmaxU = src_width / mData.mPow2Width + tminU;
		float tmaxV = src_height / mData.mPow2Height + tminV;
		FloatBuffer coordBuffer = FloatBuffer.wrap(new float[] { tminU, tminV,
				tmaxU, tminV, tminU, tmaxV, tmaxU, tmaxV, });

		GLDevice.getGL().glTexCoordPointer(2, GL10.GL_FLOAT, 0, coordBuffer);
		GLDevice.getGL().glVertexPointer(2, GL10.GL_FLOAT, 0, verticleBuffer);
		GLDevice.getGL().glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

		GLDevice.getGL().glDisableClientState(GL10.GL_VERTEX_ARRAY);
		GLDevice.getGL().glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		GLDevice.getGL().glDisable(GL10.GL_TEXTURE_2D);
		GLDevice.getGL().glLoadIdentity();

		if (mData.isAlpha || alpha != 1)
			GLDevice.disableBlend();

	}
}

/**
 * @author tianye 纹理数据描述
 */
class GLTexData {
	public boolean isBind = false;// 是否已经生成纹理
	public boolean isAlpha = false;// 是否开启透明【或者是否使用透明色】
	public boolean isKeyColor = false;// 是否使用透明色
	public WingColor keyColor;

	public int mTextureID = -1;// 纹理id，绘图使用
	public boolean isPath = false;
	public int mResourceID = -1;// 如果是从资源生成，此为资源号
	public String mPath = "";// 纹理图片路径（如果直接用路径生成）

	public int mWidth = 0;// 实际尺寸
	public int mHeight = 0;
	public int mPow2Width = 0;// 转化成2^n之后的尺寸
	public int mPow2Height = 0;
}